package com.ruoyi.project.bid.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.bid.domain.Wechat;
import com.ruoyi.project.bid.service.IWechatService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
//import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 微信管理Controller
 * 
 * @author ruoyi
 * @date 2023-11-11
 */
@Api(tags = "微信管理")
@RestController
@RequestMapping("/business/wechat")
public class WechatController extends BaseController
{
    @Autowired
    private IWechatService wechatService;

    /**
     * 查询微信列表
     */
    @ApiOperation("微信列表")
    @PreAuthorize("@ss.hasPermi('business:wechat:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wechat wechat)
    {
        startPage();
        if (!SecurityUtils.isComAdmin()) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysUser user = loginUser.getUser();
            String department = user.getDepartment();
            // 拓客部
            if ("2".equals(department)) {
                wechat.setTokerId(user.getUserId());
            } else if ("3".equals(department)) {
                // 业务部
                wechat.setBusinessId(user.getUserId());
            }
        }
        List<Wechat> list = wechatService.selectWechatList(wechat);
        return getDataTable(list);
    }

    /**
     * 用户下的微信列表
     */
    @ApiOperation("用户下的微信列表")
//    @PreAuthorize("@ss.hasPermi('business:wechat:list')")
    @GetMapping("/user/list")
    public TableDataInfo currentUserList(Wechat wechat)
    {
        startPage();
        if (!SecurityUtils.isComAdmin()) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysUser user = loginUser.getUser();
            String department = user.getDepartment();
            // 拓客部
            if ("2".equals(department)) {
                wechat.setTokerId(user.getUserId());
            } else if ("3".equals(department)) {
                // 业务部
                wechat.setBusinessId(user.getUserId());
            }
        }
        List<Wechat> list = wechatService.selectWechatList(wechat);
        return getDataTable(list);
    }

    /**
     * 导出微信列表
     */
    @PreAuthorize("@ss.hasPermi('business:wechat:export')")
    @Log(title = "微信管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Wechat wechat)
    {
        List<Wechat> list = wechatService.selectWechatList(wechat);
//        ExcelUtil<Wechat> util = new ExcelUtil<Wechat>(Wechat.class);
//        return util.exportExcel(list, "wechat");
        return null;
    }

    /**
     * 获取微信详细信息
     */
    @ApiOperation("微信详情")
    @PreAuthorize("@ss.hasPermi('business:wechat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wechatService.selectWechatById(id));
    }

    /**
     * 新增微信
     */
    @ApiOperation("新增微信")
//    @PreAuthorize("@ss.hasPermi('business:wechat:add')")
    @Log(title = "微信管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Wechat wechat)
    {
        Wechat wx = wechatService.selectWechatByWxId(wechat.getWxId());
        if (wx != null) {
            return AjaxResult.error("新增微信'" + wechat.getWxId() + "'失败，微信账号已存在");
        }

        return toAjax(wechatService.insertWechat(wechat));
    }

    /**
     * 修改微信
     */
    @ApiOperation("修改微信")
    @PreAuthorize("@ss.hasPermi('business:wechat:edit')")
    @Log(title = "微信管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Wechat wechat)
    {
        Wechat wx = wechatService.selectWechatByWxId(wechat.getWxId());
        if (wx != null && !wx.getId().equals(wechat.getId())) {
            return AjaxResult.error("修改微信'" + wechat.getWxId() + "'失败，微信账号已存在");
        }

        return toAjax(wechatService.updateWechat(wechat));
    }

    /**
     * 删除微信
     */
    @ApiOperation("删除微信")
    @PreAuthorize("@ss.hasPermi('business:wechat:remove')")
    @Log(title = "微信管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wechatService.deleteWechatByIds(ids));
    }
}
